package com.scribbles.article.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scribbles.article.dao.ArticleDAO;
import com.scribbles.article.dao.ArticleHeaderDAO;
import com.scribbles.article.util.ArticleStatusEnum;
import com.scribbles.common.entity.AppUser;
import com.scribbles.common.entity.Article;
import com.scribbles.common.entity.ArticleHeader;
import com.scribbles.user.dao.UserDAO;
import com.scribbles.user.util.UserHelper;

@Service
public class ArticleService {

	private Logger logger = Logger.getLogger(ArticleService.class);

	@Autowired
	ArticleDAO articleDAO;

	@Autowired
	ArticleHeaderDAO articleHeaderDAO;

	@Autowired
	UserDAO userDAO;

	@Transactional
	public void removeArticle(int id) {
		articleDAO.remove(articleDAO.getReference(id));
	}

	@Transactional
	public List<Article> retrieveCategories() {
		return articleDAO.findAll();
	}

	/*
	 * @Transactional public Article updateArticle(Article article) { Article
	 * articleManaged = articleDAO.findByPrimaryKey(article.getId());
	 * articleManaged.setName(article.getName());
	 * articleManaged.setArticleContent(article.getArticleContent());
	 * articleManaged.setUpdatedDate(new Date()); return articleManaged; }
	 */

	public Article getArticle(Integer articleId) {
		Article article = articleDAO.findByPrimaryKey(articleId);
		return article;
	}

	@Transactional
	public ArticleHeader createArticleHeader(ArticleHeader articleHeader) {
		AppUser createdUser = userDAO.getReference(UserHelper.getCurrentUser()
				.getId());
		AppUser updatedUser = userDAO.getReference(UserHelper.getCurrentUser()
				.getId());
		articleHeader.setCreatedBy(createdUser);
		articleHeader.setUpdatedBy(updatedUser);
		articleHeader.setCreatedDate(new Date());
		articleHeader.setUpdatedDate(new Date());
		articleHeader.setStatus(ArticleStatusEnum.DRAFT.getArticleStatus());
		articleHeader = articleHeaderDAO.merge(articleHeader);

		logger.info("ArticleHeader ID : " + articleHeader.getId());
		return articleHeader;
	}

	/**
	 * @param article
	 * @return
	 */
	@Transactional
	public Article createArticle(Article article) {
		AppUser createdUser = userDAO.getReference(UserHelper.getCurrentUser()
				.getId());
		AppUser updatedUser = userDAO.getReference(UserHelper.getCurrentUser()
				.getId());
		article.setCreatedBy(createdUser);
		article.setUpdatedBy(updatedUser);
		article.setCreatedDate(new Date());
		article.setUpdatedDate(new Date());
		article = articleDAO.merge(article);
		return article;
	}

	@Transactional
	public ArticleHeader getArticleHeaderReference(Integer articleHeaderId) {
		return articleHeaderDAO.getReference(articleHeaderId);
	}

	/**
	 * @param articleHeader
	 */
	@Transactional
	public void updateArticleHeader(ArticleHeader articleHeader) {
		ArticleHeader articleHeaderManaged = articleHeaderDAO
				.findByPrimaryKey(articleHeader.getId());
		articleHeaderManaged.setName(articleHeader.getName());
		articleHeaderManaged.setStatus(articleHeader.getStatus());
		articleHeaderManaged.setUpdatedBy(userDAO.getReference(UserHelper
				.getCurrentUser().getId()));
		articleHeaderManaged.setUpdatedDate(new Date());
	}

	@Transactional
	public Article updateArticle(Article articlePage) {
		Article articleManaged = articleDAO.findByPrimaryKey(articlePage
				.getId());
		articleManaged.setName(articlePage.getName());
		articleManaged.setArticleContent(articlePage.getArticleContent());
		articleManaged.setUpdatedBy(userDAO.getReference(UserHelper
				.getCurrentUser().getId()));
		articleManaged.setUpdatedDate(new Date());
		return articleManaged;
	}

	@Transactional
	public ArticleHeader publishArticleHeader(Integer articleHeaderId) {
		ArticleHeader articleHeaderManaged = articleHeaderDAO
		.findByPrimaryKey(articleHeaderId);
		articleHeaderManaged.setStatus(ArticleStatusEnum.PUBLISHED.getArticleStatus());
		return articleHeaderManaged;
	}
	@Transactional
	public ArticleHeader deleteArticleHeader(Integer articleHeaderId) {
		ArticleHeader articleHeaderManaged = articleHeaderDAO
		.findByPrimaryKey(articleHeaderId);
		articleHeaderManaged.setStatus(ArticleStatusEnum.DELETED.getArticleStatus());
		return articleHeaderManaged;
	}

	public ArticleHeader getArticleContentForHeaderId(Integer articleHeaderId) {
		ArticleHeader articleHeader = articleHeaderDAO.findByPrimaryKey(articleHeaderId);
		return articleHeader;
	}

	public List<Article> getArticlesListForHeader(
			Integer articleHeaderId) {
		return articleDAO.getArticlesListForHeader(articleHeaderId);
	}
}
